using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
using FairyGUI.Utils;
using System;
using JKFrame;

namespace ABA
{
    public class AudioMgr : Singleton<AudioMgr>
    {
        float musicVolume = 0.5f;
        float effectVolume = 0.5f;
        public int musicEnable = 1;
        public int effectEnable = 1;
        AudioSource audioSource;
        AudioSource _fguiAudioSource;
        protected AudioSource fguiAudioSource => _fguiAudioSource ??= Stage.inst.gameObject.GetComponent<AudioSource>();
        public void Init(GameObject gameObject)
        {
            audioSource = gameObject.AddComponent<AudioSource>();
            PlayerPrefsMgr.inst.IgnoreUid(true);
            musicVolume = (float)PlayerPrefsMgr.inst.LoadData("musicVolume", 0.5f);
            effectVolume = (float)PlayerPrefsMgr.inst.LoadData("effectVolume", 0.5f);
            musicEnable = PlayerPrefsMgr.inst.LoadData("musicenable",1);
            effectEnable = PlayerPrefsMgr.inst.LoadData("effectenable",1);
            PlayerPrefsMgr.inst.IgnoreUid(false);
            // Debug.Log("musicVolume = " + musicVolume);
            // PlayUIBg();

            _setEffectEnable_();
            SetEffectVolume(effectVolume);
            SetMusicVolume(musicVolume);
        }

        // 播放特效音接口
        public void PlayEffectFromFGUI(string path)
        {
            ////var sound = (NAudioClip)UIPackage.GetItemAssetByURL("ui://comm/" + name);
            //var sound = (NAudioClip)UIPackage.GetItemAssetByURL(path);
            //if (sound != null && sound.nativeClip != null)
            //    GRoot.inst.PlayOneShotSound(sound.nativeClip);

            PlayEffectFromFGUI(path, 0);
        }
        // startProgress：开始播放的进度，在 0 到 1 之间
        public void PlayEffectFromFGUI(string path, float startProgress)
        {
            //var sound = (NAudioClip)UIPackage.GetItemAssetByURL("ui://comm/" + name);
            if(effectEnable == 0){
                return;
            }
            var sound = (NAudioClip)UIPackage.GetItemAssetByURL(path);
            if (sound != null && sound.nativeClip != null)
            {
                GRoot.inst.PlayOneShotSound(sound.nativeClip,effectVolume);
                if(startProgress > 0)
                    SetAudioSourceTimeByNormalization(fguiAudioSource, startProgress);
            }
        }

        // 设置FGUI默认的点击音效
        public void SetButtonSoundFGUI(string path, bool isResourceLoad = false)
        {
            if(isResourceLoad)
            {
                var sound =  Resources.Load<AudioClip>(path);
                if (sound != null)
                {
                    UIConfig.buttonSound = new NAudioClip(sound);
                }                
            }
            else{
                //UIConfig.buttonSound = UIPackage.GetItemAssetByURL("ui://comm/button") as NAudioClip;
                UIConfig.buttonSound = UIPackage.GetItemAssetByURL(path) as NAudioClip;
            }
        }
    
        // 播放背景音乐接口
        public void PlayMusicFromASGO(string path, bool isResourceLoad = false)
        {
            PlayMusic(path, isResourceLoad);
        }
        public void StopMusicFromASGO()
        {
            StopMusic();
        }

        public void SetEffectVolume(float volume)
        {
            // effectVolume = volume;
            GRoot.inst.soundVolume = volume;
            PlayerPrefsMgr.inst.IgnoreUid(true);
            PlayerPrefsMgr.inst.SaveData("effectVolume", volume);
            PlayerPrefsMgr.inst.IgnoreUid(false);
        }
        public void SetMusicVolume(float volume)
        {
            musicVolume = volume;
            PlayerPrefsMgr.inst.IgnoreUid(true);
            PlayerPrefsMgr.inst.SaveData("musicVolume", volume);
            PlayerPrefsMgr.inst.IgnoreUid(false);
            audioSource.volume = musicVolume;
        }
        public void SetMusicEnable(int value,string from = "main"){
            musicEnable = value;
            PlayerPrefsMgr.inst.IgnoreUid(true);
            PlayerPrefsMgr.inst.SaveData("musicenable", value);
            PlayerPrefsMgr.inst.IgnoreUid(false);
            if(musicEnable == 0){
                // StopMusic();
                audioSource.volume = 0;
            }
            else{
                audioSource.volume = musicVolume;
                // if(from == "main"){
                //     PlayMusicFromASGO("comm_wbp776l");
                // }
                // else{
                //     PlayMusicFromASGO("comm_wbp776k");
                // }
            }
        }

        public void SetEffectEnable(int value){
            effectEnable = value;
            PlayerPrefsMgr.inst.IgnoreUid(true);
            PlayerPrefsMgr.inst.SaveData("effectenable", value);
            PlayerPrefsMgr.inst.IgnoreUid(false);
            _setEffectEnable_();
        }

        void _setEffectEnable_(){
            if(effectEnable == 0){
                GRoot.inst.DisableSound();
            }
            else{
                GRoot.inst.EnableSound();
            }
        }

        private void PlayMusic(string path, bool isResourceLoad)
        {
            if(musicEnable == 0){
                return;
            }
            //声音
            audioSource.volume = musicVolume;
            //声音判断
            if (audioSource.isPlaying)
            {
                audioSource.Stop();
            }

            if(isResourceLoad){
                var sound =  Resources.Load<AudioClip>(path);
                if (sound != null)
                {
                    audioSource.clip = sound;
                    audioSource.loop = true;
                    audioSource.Play();
                }
            }
            else{
                ResSystemYooAsset.Instance.LoadAssetAsync<AudioClip>(path, (sound) =>
                {
                    if (sound != null)
                    {
                        audioSource.clip = sound;
                        audioSource.loop = true;
                        audioSource.Play();
                    }
                });
            }
        }

        private void StopMusic(){
            if(audioSource != null) audioSource.Stop();
        }

        // normal 在 0 到 1 之间
        private void SetAudioSourceTimeByNormalization(AudioSource source, float normal)
        {
            if(normal < 0f) normal = 0f;
            else if(normal > 1f) normal = 1f;

            if (source != null)
            {
                float aSize = source.clip != null ? source.clip.length : 0;
                if(aSize != 0) aSize = aSize * 0.9999999f;
                source.time = Math.Min(normal * aSize, aSize);
            }
        }
    }
}